/*
 * CLient class for echo project
 * Author: Daniel Henderson, Fahad Muzaffar
 * TCSS 430 Summer 2013 
 * University of Washington
 */
package Client;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.Socket;

public class Client {
	/**
	 * Server are connecting with arugmets of each others.
	 * host have argument 0.
	 * host is null.
	 */
	public static String host = null;
	/**
	 * port number have argument 1.
	 * port number is 0.
	 */
	public static int portNumber = 0;
	
	public static BufferedReader br;
	/**
	 * help the client and server to communicate.
	 */
	public  static Socket socket;
	/**
	 * 
	 * @param args[1] Argument for the server IP
	 * @param args[2] Argument for the server Port
	 * @throws IOException if data is not written well.
	 */
	public static void main(String args[]) throws IOException {
		try
		{
			host = args[0];//arguments args[];
		}
		catch(ArrayIndexOutOfBoundsException e)
		{
			System.out.println("Please try again missing staring parameters");
			System.exit(0);
		}
		try
		{
			portNumber = Integer.parseInt(args[1]);
		}
		catch(ArrayIndexOutOfBoundsException e)
		{
			System.out.println("Please try again missing staring parameters");
			System.exit(0);
		}
		
		System.out.println("Creating socket to '" + host + "' on port " + portNumber);

		try {
			socket = new Socket(host, portNumber);
			System.out.println("Connection to server established.");
		} catch(IOException e){
			System.out.println("Connection to server could not be established.");
		}

		/**
		 * Inner class that handles the Receiving of Data
		 *
		 */
		class Recieve implements Runnable {

			@Override
			public void run() {
				while(true) {
					try {
						br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
					} catch (IOException e) {
						
						e.printStackTrace();
					}
					String str = null;
					try {
						str = br.readLine();
						if(str == null) {
							System.out.println("Connection with server lost....");
							System.out.println("Enter Q or q to quit");
							break;
						}
					} catch (IOException e) {
						e.printStackTrace();
						System.out.println("Disconnected from client.....quitting");
						break;
					}
					
					System.out.println("RECEIVED: " + str);
				}
				
			}
			
		}
		
		/**
		 * Inner class that handles the Sending of Data
		 *
		 */
		class Send implements Runnable {

			@Override
			public void run() {
				while(true) {
					System.out.println("SEND MESSAGE (Type Q or q to Quit): ");
					PrintWriter out = null;
					try {
						out = new PrintWriter(socket.getOutputStream(), true);
					} catch (IOException e) {
						e.printStackTrace();
					}
					BufferedReader userInputBR = new BufferedReader(new InputStreamReader(System.in));
					String userInput = null;
					try {
						userInput = userInputBR.readLine();
					} catch (IOException e) {
						e.printStackTrace();
					}
					out.println(userInput);
					
					if ("q".equalsIgnoreCase(userInput) || out == null) {
						try {
							userInputBR.close();
						} catch (IOException e) {
							e.printStackTrace();
						}
						out.close();
						try {
							socket.close();
						} catch (IOException e) {
							e.printStackTrace();
						}
						System.exit(0);
					}
				}

			}

		}
		
		Recieve reciever = new Recieve();
		new Thread(reciever).start();
		
		Send sender = new Send();
		new Thread(sender).start();

	}
}